<?php

namespace app\admin\controller;

class Packages extends Home
{
    protected static $packagesModel;

    public function __construct()
    {
        parent::__construct();
        self::$packagesModel = new \app\common\model\Packages();
    }

    /**
     * 消费包列表
     * @return mixed
     */
    public function lists()
    {
        $list = self::$packagesModel->where('status', 1)->order('id asc')->select();
        $this->assign('_list', $list);
        $this->assign('title', '消费包列表');
        return $this->fetch();
    }

    /**
     * 获取某消费包
     * @return mixed
     */
    public function one()
    {
        $id = input('id');
        $rowInfo = [];
        if ($id > 0) {
            $rowInfo = self::$packagesModel->where('id', $id)->find();
        }

        $this->assign('row_info', $rowInfo);
        $this->assign('消费包');
        return $this->fetch();
    }

    /**
     * 新增消费包
     * @return mixed
     */
    public function addOredit()
    {
        $data = request()->post();
        $id = $data['id'];
        $data['update_time'] = time();
        if ($id) {
            unset($data['id']);
            $res = self::$packagesModel->where('id', $id)->update($data);
        } else {
            $data['create_time'] = time();
            $res = self::$packagesModel->insert($data);
        }

        if ($res) {
            $this->success('操作成功！', url('lists'));
        } else {
            $this->error('操作失败！');
        }
    }

    /**
     * 删除消费包
     */
    public function del()
    {
        $ids = input('post.id');
        if (!is_array($ids)) {
            $ids = (array)$ids;
        }

        $res = self::$packagesModel->where('id', 'in', $ids)->delete();
        if ($res) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }
}